<template>
  <div class="order-list">
    <TopNavBar title="历史订单" type="back" />
    
    <!-- 顶部导航栏 -->
    <div class="tab-bar">
      <div 
        v-for="(tab, index) in tabList" 
        :key="tab.name"
        class="tab-item"
        :class="{ active: activeTab === index }"
        @click="switchTab(index)"
      >
        {{ tab.name }}
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-area">
      <template v-if="orderListByTab.length">
        <div class="order-item" v-for="item in orderListByTab" :key="item.id">
          <div class="order-header">
            <span class="stock-name">{{ item.stockName }} ({{ item.stockCode }})</span>
            <span class="direction">{{ item.direction }} | {{ item.period }}</span>
          </div>
          <div class="order-info">
            <div>下单时间：{{ item.orderTime }}</div>
            <div>预测价：{{ item.predictPrice }} → 结果价：{{ item.resultPrice }}</div>
            <div>
              盈亏：
              <span :class="['profit', item.profit >= 0 ? 'profit-up' : 'profit-down']">
                {{ item.profit >= 0 ? '+' : '' }}{{ item.profit.toFixed(2) }} USDT
              </span>
              <span class="status">状态：{{ item.status }}</span>
            </div>
          </div>
        </div>
      </template>
      <div class="empty-state" v-else>
        <div class="empty-icon">
          <img src="https://amfshe.top/assets/noData.40bd2e22.png" alt="暂无数据" />
        </div>
        <div class="empty-text">暫無數據</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import TopNavBar from '@/components/common/TopNavBar.vue'

const activeTab = ref(0)
const tabList = [
  { name: '全部訂單' },
  { name: '買漲' },
  { name: '買跌' }
]

// 假数据
const allOrders = [
  {
    id: 1,
    stockName: '特斯拉',
    stockCode: 'TSLA',
    direction: '買漲',
    period: '3天',
    orderTime: '2024-07-01 10:00',
    predictPrice: 250.00,
    resultPrice: 260.00,
    profit: 100.00,
    status: '已結算'
  },
  {
    id: 2,
    stockName: '蘋果',
    stockCode: 'AAPL',
    direction: '買跌',
    period: '1天',
    orderTime: '2024-07-02 09:30',
    predictPrice: 190.00,
    resultPrice: 185.00,
    profit: 50.00,
    status: '已結算'
  },
  {
    id: 3,
    stockName: '谷歌',
    stockCode: 'GOOGL',
    direction: '買漲',
    period: '7天',
    orderTime: '2024-07-03 14:20',
    predictPrice: 2800.00,
    resultPrice: 2750.00,
    profit: -100.00,
    status: '已結算'
  }
]

const buyUpOrders = allOrders.filter(o => o.direction === '買漲')
const buyDownOrders = allOrders.filter(o => o.direction === '買跌')

const orderListByTab = computed(() => {
  if (activeTab.value === 0) return allOrders
  if (activeTab.value === 1) return buyUpOrders
  if (activeTab.value === 2) return buyDownOrders
  return []
})

const switchTab = (index) => {
  activeTab.value = index
}
</script>

<style scoped>
.order-list {
  background: #f6f8fa;
  min-height: 100vh;
}

.tab-bar {
  display: flex;
  background: #fff;
  padding: 0 16px;
  border-bottom: 1px solid #eee;
}

.tab-item {
  flex: 1;
  text-align: center;
  padding: 16px 0;
  font-size: 16px;
  color: #888;
  cursor: pointer;
  position: relative;
  transition: color 0.2s ease;
}

.tab-item.active {
  color: #222;
  font-weight: 600;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 2px;
  background: #222;
  border-radius: 1px;
}

.content-area {
  flex: 1;
  display: flex;
  align-items: center;
  flex-direction: column;
  min-height: calc(100vh - 120px);
  padding-top:10px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.empty-icon {
  width: 120px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-icon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.empty-text {
  font-size: 16px;
  color: #666;
  font-weight: 500;
}

.order-item {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.03);
  padding: 16px;
  margin-bottom: 16px;
  width: 340px;
  max-width: 90vw;
}
.order-header {
  display: flex;
  justify-content: space-between;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}
.stock-name {
  color: #222;
}
.direction {
  color: #888;
  font-size: 14px;
}
.order-info {
  font-size: 14px;
  color: #555;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.profit {
  font-weight: 600;
  margin-right: 12px;
}
.profit-up {
  color: #1bc47d;
}
.profit-down {
  color: #f44336;
}
.status {
  margin-left: 8px;
  color: #888;
  font-size: 13px;
}
</style> 